var util = require('../../utils/util.js');
const {
  formatTime
} = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputValue:'',
    isOpen:false,
    showDialog: true,
    isDefTap: true,
    hasUserInfo: false,
    matchPage: {
      loading: false,
      pageNum: 0,
      now_page_no: 1,
      data: [],
      noMore: false,
    },
    positionPage: {
      loading: false,
      pageNum: 0,
      now_page_no: 1,
      data: [],
      noMore: false,
    },
    currentTab: 0,
    banner: {},
    toppositioninfo: {},
    tophunterinfo: {},
    topartinfo: {},
    channel: {},
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    isShow: false, //为true表示openid和userinfo都有，即已登陆
    isScroll: true,
    nonet: true,
    test: {},
    pageHot: {},
    pagenation: {},
    navigate_type: '', //分类类型，是否包含二级分类
    slideWidth: '', //滑块宽
    slideLeft: 0, //滑块位置
    totalLength: '', //当前滚动列表总长
    slideShow: false,
    slideRatio: '',
    searchData: "",
    option1: [{
      text: '全部领域',
      value: 0
    }],
    option2: [{
      text: '全部地区',
      value: 0
    }],
    option3: [{
        text: '薪资范围',
        value: 0
      },
      {
        text: '10-20W',
        value: 1
      },
      {
        text: '20-30W',
        value: 2
      },
      {
        text: '30-50W',
        value: 3
      },
      {
        text: '50-100W',
        value: 4
      },
      {
        text: '100W以上',
        value: 5
      },
    ],
    value1: 0,
    value2: 0,
    value3: 0,
    swiperArr:[1,2,3,4,5,6,7,8,9,10]
  },

  sidebarDrawer: function(e){
    var currentStatu = e.currentTarget.dataset.statu;
    this.side(currentStatu)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  toggleCityValue: function({ detail }){
    this.setData({
      value2: detail
    })
    this.getJobData();
  },

  toggleSalaryValue: function({ detail }){
    this.setData({
      value3: detail
    })
    this.getJobData();
  },

  toggleDomainValue: function ({ detail }) {
    this.setData({
      value1: detail
    })
    this.getJobData();
  },

  getJobData(){
    var that = this;
    console.log(this.data.value1,'------',this.data.value2,'========',this.data.value3)
    util.post(app.api.getTopJobByHot, {
      pageSize: 10,
      domain: this.data.value1,
      city: this.data.value2,
      salary: this.data.value3,
      keyWord: this.data.searchData
    }, function (res) {
      let result = res.data;
      result.forEach(function (item, index) {
        item.createDate = util.date_format_time(item.createDate);
      })
      that.setData({
        toppositioninfo: result
      });
    });
  },

  onShow: function (options) {
    var that = this;
    that.setData({
      value1: 0,
      value2: 0,
      value3: 0
    })
    //是否登陆
    if (!app.hasLogin()) {
    } else {
      that.setData({
        userInfo: app.getUserInfo(),
        isShow: true,
        showDialog: false,
      });
    }
    that.getCity();
    that.getDomains();
    that.setInfo();
  },

  getDomains() {
    var that = this;
    var domainList = that.data.option1;
    util.post(app.api.getDomains, {}, function (res) {
      let result = res.data;
      const domains = domainList.concat(result);
      that.setData({
        option1: domains
      })
    })
  },

  getCity() {
    var that = this;
    var cityList = that.data.option2;
    console.log(cityList);
    util.post(app.api.getCities, {}, function (res) {
      let result = res.data;
      const cities = cityList.concat(result);
      that.setData({
        option2: cities
      })
    })
  },
  
  setInfo() {
    var that = this;
    if (app.globalData.userInfo != null) {
      that.setData({
        hasUserInfo: true
      });
    }
    that.resetData(true);
    util.post(app.api.getAdcarouse, {
      type: 1
    }, function (res) {
      //获取首页轮播图
      that.setData({
        banner: res.data
      });

    });
    //展示前3条职位信息
    util.post(app.api.getTopJobByHot, {
      pageSize: 10,
      keyWord: this.data.searchData
    }, function (res) {
      let result = res.data;
      result.forEach(function (item, index) {
        item.createDate = util.date_format_time(item.createDate);
      })
      that.setData({
        toppositioninfo: result
      });
    });
    //展示前三条文章
    util.post(app.api.getTopArticleByHot, {
      pageSize: 3
    }, function (res) {

      that.setData({
        pageHot: res.data
      })
    });
    //展示前3条轮播广告信息
    util.post(app.api.getChannel, {}, function (res) {
      that.setData({
        channel: res.data
      });
    });

    //test
    util.post(app.api.toWebOauthTest, {}, function (res) {
      that.setData({
        test: res.data
      });

    });
    if (app.globalData.openid != null) {
      util.post(app.api.isHaveResume, {
        unionid: app.globalData.unionid
      }, function (res) {
        if (res.data != null) {
          that.setData({
            isHaveResume: res.data
          });
          wx.setStorageSync('isHaveResume', that.data.isHaveResume);
        }
      });
    }
  },

  tapTo: function (event) {
    let url = event.target.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },

  //职位详情
  positionDetailTap: function (event) {
    var id = event.currentTarget.dataset.id; // 当前id

    // 切换页面
    wx.navigateTo({
      url: '../position-detail/position-detail?pid=' + id
    });
  },
  //文章详情
  clickTit: function (event) {
    var url = event.currentTarget.dataset.id
    console.log(event)
    console.log("clickTit" + url)
    wx.navigateTo({
      url: '/pages/articleContent/index?url=' + url,
    })


    // wx.navigateToMiniProgram({ appId: "wx55e844e819182d5a", path: "pages/leader/stories/stories?url="+id})
    // wx.navigateTo({
    //   url: '/pages/articleContent/index?url=' +id
    // });
  },
  //更多文章
  moreart: function () {
    wx.navigateToMiniProgram({
      appId: "wx55e844e819182d5a",
      path: "pages/leader/leader"
    })
  },
  //更多职位
  morepositonTap: function (event) {
    wx.navigateTo({
      url: '/pages/home/home',
    })
  },
  //match position 专属职位
  matchPositionTap: function (event) {
    if (app.globalData.openid && this.data.userInfo && this.data.isShow) {
      if (this.data.isHaveResume) {
        wx.navigateTo({
          url: '/pages/position/match',
        })
      } else {
        wx.showModal({
          title: "完善资料",
          content: '请您先完善资料',
          cancelText: '取消',
          cancelColor: '#999',
          confirmText: '确认',
          confirmColor: '#4990E2',
          success: function (res) {
            if (res.confirm) {
              wx.reLaunch({
                url: '/pages/user/index',
                //url: '/pages/web-view/web-view'
                // url: '/pages/edit-resume-base/edit-resume-base?type=0',
              });
            } else {
              return;
            }
          }
        })
      }

    } else {
      wx.showModal({
        title: '请先登录',
        content: '请您先登录',
        cancelText: '取消',
        cancelColor: '#999',
        confirmText: '确认',
        confirmColor: '#4990E2',
        success: function (res) {
          if (res.confirm) {
            wx.reLaunch({
              url: '/pages/user/index',
              //url: '/pages/web-view/web-view'
              // url: '/pages/edit-resume-base/edit-resume-base?type=0',
            });
          } else {
            return;
          }
        }
      })
    }



  },

  interTop5Tap: function (event) {
    wx.navigateTo({
      url: '/pages/positionTop5/index',
    })
  },
  hiPay: function (event) {
    wx.navigateTo({
      url: '/pages/hiPay/index',
    })
  },
  newIndustry: function (event) {
    wx.navigateTo({
      url: '/pages/newIndustry/index',
      // url: '/pages/publicity-position/index',
    })
  },
  // searchInp: function (event) {
  //   wx.navigateTo({
  //     url: '/pages/search/search?pid=' + 2 + '&psearch=2',
  //   })
  // },
  /** 
   * 点击tab切换 
   */
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      });
      var isDefTap = true;
      if (e.target.dataset.current != 0) {
        isDefTap = false;
      }
      that.setData({
        isDefTap: isDefTap
      });
    }
  },
  setInputValue(e){
    this.setData({
      inputValue:e.detail.value
    })
  },
  searchInput: function(e) {
    var text = e.detail.value;
    console.log(e.detail.value)
    if (typeof (text) == "undefined" || text.length == 0) {
      this.setData({
        'searchData':this.data.inputValue
      })
    }else{
      this.setData({
        'searchData': text
      })
    }
    this.searchData();
  },
  searchData () {
    var that = this;
    util.post(app.api.getTopJobByHot, {
      pageSize: 10,
      keyWord: this.data.searchData
    }, function (res) {
      let result = res.data;
      result.forEach(function (item, index) {
        item.createDate = util.date_format_time(item.createDate);
      })
      that.setData({
        toppositioninfo: result,
        value1: 0,
        value2: 0,
        value3: 0
      });
    });
  },
  resetDataForPosition: function (url, now_page_no, isappend) {
    var that = this;
    var currObj = that.data.positionPage;
    util.post(url, {
      page: now_page_no,
      size: 5
    }, function (res) {
      if (res.data == null) {
        wx.showToast({
          title: '正在加载中......',
          icon: 'loading',
          duration: 1200
        })
        return;
      }
      if (isappend) {
        //上拉加载更多
        that.setData({
          'positionPage.loading': false
        });
        now_page_no = now_page_no + 1;
        var data = [];
        for (var item in currObj.data) {
          data.push(currObj.data[item]);
        }
        for (var item in res.data.data) {
          data.push(res.data.data[item]);
        }

      } else {
        that.setData({
          'positionPage.noMore': false
        });
        //下拉刷新 此处停止刷新效果  重置now_page_no为1
        now_page_no = 1;
        wx.stopPullDownRefresh();
        var data = res.data.data;
        //wx.showToast({ title: '刷新了' });
      }
      that.setData({
        'positionPage.pageNum': res.data.pageNum
      });
      that.setData({
        'positionPage.data': data,
        'positionPage.now_page_no': now_page_no
      });
    });
  },

  resetData: function (isOnLoad, isappend, now_page_no) {
    var isappend = isappend == undefined ? false : true;
    var now_page_no = now_page_no == undefined ? 1 : now_page_no;
    var url = "";
    if (isOnLoad) {
      //如果是onload函数中执行此函数，那么resetDataForPosition和resetDataForMatch都要执行
      this.resetDataForPosition(app.api.getJob, now_page_no, isappend);
      // this.resetDataForMatch(app.api.getList, now_page_no, isappend);
    } else {
      if (this.data.currentTab == 0) {
        //最新职位
        url = app.api.getJob;
        this.resetDataForPosition(url, now_page_no, isappend);
      } else {
        //精准匹配
        // url = app.api.getList;
        //this.resetDataForMatch(url, now_page_no, isappend);
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  // onShow: function () {

  // },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.resetData(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var currObj = null;
    if (this.data.currentTab == 0) {
      //最新职位
      currObj = this.data.positionPage;
    } else {
      //精准匹配
      currObj = this.data.matchPage;
    }

    if (currObj.loading) return;
    if (currObj.now_page_no >= currObj.pageNum) {

      if (this.data.currentTab == 0) {
        //最新职位
        this.setData({
          'positionPage.noMore': true
        });
      } else {
        //精准匹配
        this.setData({
          'matchPage.noMore': true
        });
      }

    } else {
      this.resetData(false, 1, currObj.now_page_no);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    // var shareObj = {
    //   title:'主页转发的标题'
    // }
    // return shareObj;
  },
  onOpen(){
    this.setData({
      isOpen:true
    })
  },
  onClose(){
    this.setData({
      isOpen:false
    })
  },
  ondisble(){
    this.selectComponent('#mydropdown').close();
    this.setData({
      isOpen:false
    })
  },
  /**
   * banner 跳转操作
   */
  bannerLinkTo: function(event){
    var linkParam = event.target.dataset.url;
    //判断是否是校招banner
    if(linkParam == 123) {
      //判断是否授权
      if(app.globalData.unionid) {
        //判断是否完善用户信息
        var url = app.api.getSchPersonByUnionid+"/"+app.globalData.unionid;
        util.post(url,null,function(res){
          if (res.data) {
            console.log(res.data)
            //跳转校招职位列表
            wx.navigateTo({
              url: '/pages/schJob/index',
              // url: '/pages/schPerson/index?tag=1',
            })
          } else {
            //完善校招用户信息
            wx.showModal({
              title: '完善信息',
              content: '请您先完善您的资料',
              cancelText: '取消',
              cancelColor: '#999',
              confirmText: '确认',
              confirmColor: '#4990E2',
              success: function (res) {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '/pages/schPerson/index?tag=1',
                  })
                } 
              }
            })
          }
        })
      } else {
        //提示授权
        wx.showModal({
          title: '登录提醒',
          content: '请微信授权登录,再完善您的资料',
          cancelText: '取消',
          cancelColor: '#999',
          confirmText: '确认',
          confirmColor: '#4990E2',
          success: function (res) {
            if (res.confirm) {
              app.loading();
              wx.reLaunch({
                url: '/pages/user/index',
              })
            }
          }
        })
      }
    }
  }
})